#!/bin/sh

# Must set 'BASE' environement variable
# to tremulous base directory

if [ `uname` != "Linux" ] || [ `uname -m | sed -e s/i.86/x86/` != "x86" ];
then
    echo "Error: works only on linux x86 system"
    exit 1
fi

if [ ! "$BASE" ];
then
    echo "Please set 'BASE' environement variable to base path"
    exit 1
fi

PWD=`pwd`
RELEASE=`basename $PWD`
OUT_DIR=release

COMMON_DATA="ChangeLog COPYING GPL"
FULL_RELEASE_DATA="CC"
LINUX_DATA="misc/tremulous.xpm misc/tremulous-server"
WIN_DATA="misc/tremulous.ico misc/tremulous-server.bat"

# Make linux binary files
make || exit 1

# Make windows binary files
sh cross-make-mingw.sh || exit 1

function copy_unix_binaries ()
{
    mkdir -p $1
    mkdir -p $1/base
    mkdir -p $1/base/vm

    cp $2/tremded.$3 $1
    cp $2/base/game$3.so $1/base
    cp $2/base/vm/game.qvm $1/base/vm
}

function copy_win_binaries ()
{
    mkdir -p $1
    mkdir -p $1/base
    mkdir -p $1/base/vm

    cp $2/tremded.$4.exe $1
    cp $2/base/game$4.dll $1/base
    cp $3/base/vm/game.qvm $1/base/vm
}

LINUX_RELEASE_DIR=build/release-linux-x86
WIN32_RELEASE_DIR=build/release-mingw32-x86

# FULL LINUX X86 RELEASE
BASE_DIR=${OUT_DIR}/${RELEASE}_linux_x86_full
copy_unix_binaries $BASE_DIR $LINUX_RELEASE_DIR "x86"
cp -r $BASE/* $BASE_DIR/base
cp $COMMON_DATA $BASE_DIR
cp $LINUX_DATA $BASE_DIR
cp $FULL_RELEASE_DATA $BASE_DIR
tar czf $BASE_DIR.tar.gz -C $BASE_DIR/.. `basename $BASE_DIR`

# BINARY LINUX X86 RELEASE
BASE_DIR=${OUT_DIR}/${RELEASE}_linux_x86_bin
copy_unix_binaries $BASE_DIR $LINUX_RELEASE_DIR "x86"
cp $COMMON_DATA $BASE_DIR
cp $LINUX_DATA $BASE_DIR
tar czf $BASE_DIR.tar.gz -C $BASE_DIR/.. `basename $BASE_DIR`

# FULL LINUX X86 RELEASE
BASE_DIR=${OUT_DIR}/${RELEASE}_linux_win32_full
copy_win_binaries $BASE_DIR $WIN32_RELEASE_DIR $LINUX_RELEASE_DIR "x86"
cp -r $BASE/* $BASE_DIR/base
cp $COMMON_DATA $BASE_DIR
cp $WIN_DATA $BASE_DIR
cp $FULL_RELEASE_DATA $BASE_DIR
zip -r -b $BASE_DIR/.. $BASE_DIR.zip $BASE_DIR

# BINARY LINUX X86 RELEASE
BASE_DIR=${OUT_DIR}/${RELEASE}_linux_win32_bin
copy_win_binaries $BASE_DIR $WIN32_RELEASE_DIR $LINUX_RELEASE_DIR "x86"
cp $COMMON_DATA $BASE_DIR
cp $WIN_DATA $BASE_DIR
zip -r -b $BASE_DIR/.. $BASE_DIR.zip $BASE_DIR

